package com.example.lotterysystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.lotterysystem.common.enums.GlobalErrorCodeEnum;
import com.example.lotterysystem.common.exception.GlobalException;
import com.example.lotterysystem.controller.param.CreatePrizeParam;
import com.example.lotterysystem.controller.param.PageParam;
import com.example.lotterysystem.dao.dataobject.ActivityPrize;
import com.example.lotterysystem.dao.dataobject.Prize;
import com.example.lotterysystem.mapper.ActivityPrizeMapper;
import com.example.lotterysystem.mapper.PrizeMapper;
import com.example.lotterysystem.service.PictureService;
import com.example.lotterysystem.service.PrizeService;
import com.example.lotterysystem.service.dto.PageListDTO;
import com.example.lotterysystem.service.dto.PrizeDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class PrizeServiceImpl implements PrizeService {

    @Autowired
    private PictureService pictureService;
    @Autowired
    private PrizeMapper prizeMapper;
    @Autowired
    private ActivityPrizeMapper activityPrizeMapper;
    @Override
    public Long createPrize(CreatePrizeParam param, MultipartFile file) {
        if(prizeMapper.selectOne(new QueryWrapper<Prize>()
                .eq("name", param.getPrizeName())) != null){
            throw new GlobalException(GlobalErrorCodeEnum.PRIZE_CREATE_ERROR, "奖品已经存在");
        }

        // 上传图片
        String fileName = pictureService.savePicture(file);
        Prize prize = new Prize();
        prize.setName(param.getPrizeName());
        prize.setDescription(param.getDescription());
        prize.setPrice(param.getPrice());
        prize.setImageUrl(fileName);
        prizeMapper.insert(prize);
        // 存储到数据库
        return prize.getId();
    }

    @Override
    public PageListDTO<PrizeDTO> getPrizeList(PageParam param) {
        // 查询总量
        int total = prizeMapper.selectCount(new QueryWrapper<>()).intValue();

        // 查询当前列表页
//        Page<Prize> page = new Page<>(param.getCurrentPage(), param.getPageSize());
//        page = prizeMapper.selectPage(page, new QueryWrapper<>());
//        List<PrizeDTO> prizeDTOList = page.getRecords().stream()
//                .map(this::convertToPrizeDTO)
//                .collect(Collectors.toList());
        // 使用mybatis
        List<PrizeDTO> prizeDTOS = new ArrayList<>();
        List<Prize> prizeList = prizeMapper.selectPrizeList(param.getOffset(), param.getPageSize());
        for (Prize prize : prizeList){
            PrizeDTO prizeDTO = new PrizeDTO();
            prizeDTO.setPrizeId(prize.getId());
            prizeDTO.setPrizeName(prize.getName());
            prizeDTO.setDescription(prize.getDescription());
            prizeDTO.setPrice(prize.getPrice());
            prizeDTO.setImageUrl(prize.getImageUrl());
            prizeDTOS.add(prizeDTO);
        }
        return new PageListDTO<>(total, prizeDTOS);
    }

    @Override
    public boolean deletePrizeById(Long prizeId) {
        if(prizeId == null){
            throw new GlobalException(GlobalErrorCodeEnum.PRIZE_DELETE_ERROR, "奖品id为空");
        }
        List<ActivityPrize> activityPrizeList = activityPrizeMapper.selectList(
                new QueryWrapper<ActivityPrize>()
                        .eq("prize_id", prizeId)
                        .eq("status", "INIT"));
        if(!CollectionUtils.isEmpty(activityPrizeList)){
            throw new GlobalException(GlobalErrorCodeEnum.PRIZE_DELETE_ERROR, "要删除的奖品在抽奖活动中未抽奖, 请抽奖之后在执行此操作");
        }
        int result = prizeMapper.deleteById(prizeId);
        return result == 1;
    }

    @Override
    public boolean batchDeletePrizesByIds(List<Long> prizeIds) {
        if(CollectionUtils.isEmpty(prizeIds)){
            throw new GlobalException(GlobalErrorCodeEnum.PRIZE_DELETE_ERROR);
        }
        List<ActivityPrize> activityPrizeList = activityPrizeMapper.selectList(
                new QueryWrapper<ActivityPrize>()
                        .eq("status", "INIT")
                        .in("prize_id", prizeIds));
        if(!CollectionUtils.isEmpty(activityPrizeList)){
            throw new GlobalException(GlobalErrorCodeEnum.PRIZE_DELETE_ERROR, "要删除的奖品在抽奖活动中未抽奖, 请抽奖之后在执行此操作");
        }
        int result = prizeMapper.deleteBatchIds(prizeIds);
        return result > 0;
    }
}
